package com.fyt.ZhongYaoApp.service.impl;

import com.fyt.ZhongYaoApp.mapper.EssayMapper;
import com.fyt.ZhongYaoApp.pojo.Browsingrecord;
import com.fyt.ZhongYaoApp.pojo.Essay;
import com.fyt.ZhongYaoApp.pojo.PageBean;
import com.fyt.ZhongYaoApp.pojo.User;
import com.fyt.ZhongYaoApp.pojo.pg.EssayPG;
import com.fyt.ZhongYaoApp.service.EssayService;
import com.fyt.ZhongYaoApp.service.UserService;
import com.fyt.ZhongYaoApp.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class EssayServiceImpl implements EssayService {

    @Autowired
    private EssayMapper essayMapper;

    @Autowired
    private UserService userService;

    /**
     * 查询文章列表
     * @return
     */
    @Override
    public List<Essay> EssayList() {
        List<Essay> list = essayMapper.EssayList();
        return list;
    }

    /**
     * 根据文章id查看文章详情
     * @param id
     * @return
     */
    @Override
    public Essay findEssayById(Integer id) {

        // 调用mapper，根据文章id查看文章详情
        Essay essay = essayMapper.findEssayById(id);

        // 封装实体
        Browsingrecord browsingrecord = new  Browsingrecord();
        browsingrecord.setEssayId(id);

        // 调用浏览记录模块
        addBrowsingrecord(browsingrecord);
        return essay;
    }

    /**
     * 添加浏览记录
     * @param browsingrecord
     * @return
     */
    @Override
    public Browsingrecord addBrowsingrecord(Browsingrecord browsingrecord) {
        /*
        封装用户id
         */
        Integer userId;
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String)map.get("username");
        User user = userService.findByUserName(username);
        userId = user.getId();
        browsingrecord.setUserId(userId);

        // 判断是否存在此文章记录
        Browsingrecord browsingrecord1 = essayMapper.findAccessTimeIfExisByUserId(browsingrecord);
        if(browsingrecord1==null){// 如果没有记录，则添加新记录
            essayMapper.addBrowsingrecord(browsingrecord);
        }else{// 否则修改记录时间
            essayMapper.updateAccessTime(browsingrecord);
        }
        return browsingrecord;
    }

    /**
     * 根据用户id查询浏览记录
     * @param essayPG
     * @return
     */
    @Override
    public PageBean<Essay> findAccessTimeByUserId(EssayPG essayPG) {

        /*
        封装用户id
         */
        Integer userId;
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String)map.get("username");
        User user = userService.findByUserName(username);
        userId = user.getId();

        // 创建PageBean对象
        PageBean<Essay> pageBean = new PageBean();

        // 开启分页查询pageHelper
        PageHelper.startPage(essayPG.getPageNum(),essayPG.getPageSize());

        List<Essay> list = essayMapper.findAccessTimeByUserId(userId);

        Page<Essay> page = (Page<Essay>) (list);

        // 把数据填充到PageBean对象中
        pageBean.setTotal(page.getTotal());
        pageBean.setItems(page.getResult());

        return pageBean;
    }
}
